#include "customtitlebar.h"

#include <QHBoxLayout>
#include <QStackedLayout>
#include <QMenuBar>
#include <QLabel>

CustomTitleBar::CustomTitleBar(QWidget *parent, bool is_max)
    : QWidget{parent}
    , m_parent(parent)
    , m_is_max(is_max)
{
    setFixedHeight(32);
    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);

    // 添加图标
    QLabel * appIcon = new QLabel(this);
    appIcon->setStyleSheet("QLabel {"
                              "   border-image: url(:/images/codemaster.bmp);"
                              "}"
                              "background-repeat: no-repeat;"
                              "border: none;"
                              "}");

    // 添加菜单栏
    QMenuBar *menuBar = new QMenuBar(this);
    menuBar->setMinimumWidth(400);


     // 添加关闭按钮
    this->m_closeWindow = new QPushButton(this);
    this->m_closeWindow->setStyleSheet("QPushButton {"
                                       "   border-image: url(:/images/windows/close(w11)_normal.bmp);"
                                       "}"
                                       "QPushButton:hover {"
                                       "   border-image: url(:/images/windows/close(w11)_moveon.bmp);"
                                       "}"
                                       "QPushButton:pressed {"
                                       "   border-image: url(:/images/windows/close(w11)_click.bmp);"
                                       "background-repeat: no-repeat;"
                                       "border: none;"
                                       "}");
    this->m_closeWindow->setFixedHeight(32);
    this->m_closeWindow->setFixedWidth(32);

    QStackedLayout * maxwindow_layout = new QStackedLayout(this);

     // 添加最大化按钮
    this->m_maxWindow = new QPushButton(this);
    this->m_maxWindow->setStyleSheet("QPushButton {"
                                     "   border-image: url(:/images/windows/uptomax(w11)_normal.bmp);"
                                     "}"
                                     "QPushButton:hover {"
                                     "   border-image: url(:/images/windows/uptomax(w11)_moveon.bmp);"
                                     "}"
                                     "QPushButton:pressed {"
                                     "   border-image: url(:/images/windows/uptomax(w11)_clicked.bmp);"
                                     "background-repeat: no-repeat;"
                                     "border: none;"
                                     "}");
    this->m_maxWindow->setFixedHeight(32);
    this->m_maxWindow->setFixedWidth(32);

    this->m_normalWindow = new QPushButton(this);
    this->m_normalWindow->setStyleSheet("QPushButton {"
                                     "   border-image: url(:/images/windows/downmax(w11)_normal.bmp);"
                                     "}"
                                     "QPushButton:hover {"
                                     "   border-image: url(:/images/windows/downmax(w11)_moveon.bmp);"
                                     "}"
                                     "QPushButton:pressed {"
                                     "   border-image: url(:/images/windows/downmax(w11)_click.bmp);"
                                     "background-repeat: no-repeat;"
                                     "border: none;"
                                     "}");
    this->m_normalWindow->setFixedHeight(32);
    this->m_normalWindow->setFixedWidth(32);

    maxwindow_layout->addWidget(m_maxWindow);
    maxwindow_layout->addWidget(m_normalWindow);


    // 添加最小化按钮
    this->m_minWindow = new QPushButton(this);
    this->m_minWindow->setStyleSheet("QPushButton {"
                                     "   border-image: url(:/images/windows/min(w11)_normal.bmp);"
                                     "}"
                                     "QPushButton:hover {"
                                     "   border-image: url(:/images/windows/min(w11)_moveon.bmp);"
                                     "}"
                                     "QPushButton:pressed {"
                                     "   border-image: url(:/images/windows/min(w11)_clicked.bmp);"
                                     "background-repeat: no-repeat;"
                                     "border: none;"
                                     "}");
    this->m_minWindow->setFixedHeight(32);
    this->m_minWindow->setFixedWidth(32);

    layout->addWidget(appIcon);
    layout->addWidget(menuBar);
    layout->addWidget(m_minWindow);
    layout->addLayout(maxwindow_layout);
    layout->addWidget(m_closeWindow);

    m_minWindow->show();
    if(m_is_max) {
        m_maxWindow->hide();
        m_normalWindow->show();
    }
    else
    {
        m_maxWindow->show();
        m_normalWindow->hide();
    }
    m_closeWindow->show();

    connect(this->m_closeWindow, &QPushButton::clicked, this, &CustomTitleBar::OnClose);
    connect(this->m_minWindow, &QPushButton::clicked, this, &CustomTitleBar::OnMin);
    connect(this->m_maxWindow, &QPushButton::clicked, this, &CustomTitleBar::OnMax);
    connect(this->m_normalWindow, &QPushButton::clicked, this, &CustomTitleBar::OnNormal);
}

void CustomTitleBar::OnClose()
{
    m_parent->close();

}

void CustomTitleBar::OnMin()
{
    if(m_parent)
    {
        m_parent->showMinimized();
    }
}

void CustomTitleBar::OnNormal()
{
    if(m_parent)
    {
        m_parent->showNormal();
        m_maxWindow->show();
        m_normalWindow->hide();
        m_is_max = true;
    }

}
void CustomTitleBar::OnMax()
{

    if(m_parent)
    {
        m_parent->showMaximized();

        m_maxWindow->hide();
        m_normalWindow->show();
        m_is_max = false;
    }
}
